package com.yiren.dbaa.module.practice_set.carmodel.view.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.yiren.backstage.currency.entity.CarModelEntity;
import com.yiren.dbaa.R;
import com.yiren.dbaa.activity.BaseActivity;
import com.yiren.dbaa.module.practice_set.carmodel.contract.CarModelManageContract;
import com.yiren.dbaa.module.practice_set.carmodel.enums.AntennaLocationEnum;
import com.yiren.dbaa.module.practice_set.carmodel.enums.AntennaTypeEnum;
import com.yiren.dbaa.module.practice_set.carmodel.presenter.CarModelManagePresenter;
import com.yiren.dbaa.module.practice_set.site.view.dialog.SelectUserDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author lwb
 * @date : 2022/12/6 10:51
 * @description: 车模管理
 */
public class CarModelManageActivity extends BaseActivity implements CarModelManageContract.View {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_carmodel_name)
    TextView tvCarmodelName;
    @BindView(R.id.tv_carmodel_version)
    TextView tvCarmodelVersion;
    @BindView(R.id.tv_carmodel_version_bt)
    TextView tvCarmodelVersionBt;
    @BindView(R.id.tv_using_range)
    TextView tvUsingRange;
    @BindView(R.id.tv_using_range_bt)
    TextView tvUsingRangeBt;
    @BindView(R.id.tv_release)
    TextView tvRelease;
    @BindView(R.id.tv_edit_carmodel)
    TextView tvEditCarmodel;

    private Intent intent;
    private CarModelEntity carModel;
    private CarModelManageContract.Presenter managePresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manage_carmodel);
        ButterKnife.bind(this);
        tvTitle.setText("车模数据管理");
        intent = getIntent();
        carModel = (CarModelEntity) intent.getSerializableExtra("carModel");
        init();
    }

    private void init() {
        managePresenter = new CarModelManagePresenter(this);
        if (carModel != null) {
            String name = carModel.getName() + "-" + AntennaTypeEnum.getValue(carModel.getAntennaType()) + "-" + AntennaLocationEnum.getValue(carModel.getPositioned());
            tvCarmodelName.setText(name);
            tvCarmodelVersion.setText("V" + carModel.getCarModelVer());
            if (carModel.getUseRange() == 1) {
                tvUsingRange.setText("个别用户使用");
            } else {
                tvUsingRange.setText("全部用户使用");
            }
            if (carModel.getCarModelVer() != 0) {
                tvCarmodelVersionBt.setText("回退版本");
                tvCarmodelVersionBt.setTextColor(getResources().getColor(R.color.white));
                tvCarmodelVersionBt.setBackgroundResource(R.drawable.btn_blue_radius_selector);
                if (carModel.getUseRange() == 1) {
                    tvUsingRangeBt.setText("修改范围");
                    tvUsingRangeBt.setTextColor(getResources().getColor(R.color.white));
                    tvUsingRangeBt.setBackgroundResource(R.drawable.btn_blue_radius_selector);
                } else {
                    tvUsingRangeBt.setText("已是全部");
                    tvUsingRangeBt.setTextColor(getResources().getColor(R.color.gray_text));
                }
            } else {
                tvUsingRange.setText("无用户使用");
            }
        }
    }

    @OnClick({R.id.iv_back, R.id.tv_carmodel_version_bt, R.id.tv_using_range_bt, R.id.tv_release, R.id.tv_edit_carmodel})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_carmodel_version_bt:
                intent = new Intent(this, CarModelHistoryActivity.class);
                Bundle bundle2 = new Bundle();
                bundle2.putSerializable("carModel", carModel);
                intent.putExtras(bundle2);
                startActivity(intent);
                break;
            case R.id.tv_using_range_bt:
                if (carModel.getUseRange() == 1) {
                    List<String> user = new ArrayList<>();
                    user.add("全部用户使用");
                    user.add("个别用户使用");
                    SelectUserDialog.show(this, "修改范围", carModel.getUseRange() == 1 ? 1 : 0, user, new SelectUserDialog.OnSureClickListener() {
                        @Override
                        public void onSureClick(int selectIndex) {
                            managePresenter.changeUserRange(CarModelManageActivity.this, carModel, selectIndex == 0 ? 2 : 1);
                        }
                    });
                }
                break;
            case R.id.tv_release:
                intent = new Intent(this, CarModelReleaseActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("carModel", carModel);
                intent.putExtras(bundle);
                startActivity(intent);
                break;
            case R.id.tv_edit_carmodel:
                intent = new Intent(this, CarModelCollectActivity.class);
                Bundle bundle1 = new Bundle();
                bundle1.putSerializable("carModel", carModel);
                intent.putExtras(bundle1);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void updateUseRange(int range) {
        if (range == 2) {
            tvUsingRange.setText("全部用户使用");
            tvUsingRangeBt.setText("已是全部");
            tvUsingRangeBt.setBackground(null);
            tvUsingRangeBt.setTextColor(getResources().getColor(R.color.gray_text));
        } else {
            tvUsingRange.setText("个别用户使用");
        }
    }

}